package es.caeboxapps.game;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphUser;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.games.GamesClient;
import com.google.android.gms.games.Player;
import com.google.example.games.basegameutils.BaseGameActivity;

public class GameOverActivity extends BaseGameActivity {

	private TextView puntuacion;
	String textoPuntuacion="";
	GamesClient mGamesClient;
	private int REQUEST_LEADERBOARD = 0;
	private int REQUEST_DIALOG = 1;
	
	private void seleccionarOpcion(Boolean jugarOtraPartida) {
		Intent intent = new Intent();
		Intent intent1 = intent.putExtra("jugarOtraPartida", jugarOtraPartida);
		setResult(-1, intent);
		finish();
	}

	@SuppressLint("NewApi")
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
//		boolean flag = requestWindowFeature(1);
		this.setFinishOnTouchOutside(false);
		
		setContentView(R.layout.game_over);
		Button buttonSi = (Button) findViewById(R.id.buttonSi);
		Button buttonNo = (Button) findViewById(R.id.buttonNo);
		TextView texto = (TextView) findViewById(R.id.textoGameOver);
		puntuacion = (TextView) findViewById(R.id.textoPuntos);

		// Set Type Face
		Typeface font = Typeface.createFromAsset(getAssets(),
				"Bunaken_Underwater.ttf");
		texto.setTypeface(font);
		buttonSi.setTypeface(font);
		buttonNo.setTypeface(font);
		puntuacion.setTypeface(font);
		
		
		
		int isGooglePlayServiceAvilable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
		if (isGooglePlayServiceAvilable == ConnectionResult.SUCCESS) {
		    beginUserInitiatedSignIn();
		} else {
		    GooglePlayServicesUtil.getErrorDialog(isGooglePlayServiceAvilable, GameOverActivity.this, REQUEST_DIALOG).show();
		}
		
		
		
//		if(getGamesClient().isConnected()){
//			
//		}else{
//			showAlert("No conectado a Google, no se ha podido actualizar tu puntuacion");
////			Toast.makeText(GameOverActivity.this,"No Conectado a Google",Toast.LENGTH_LONG).show();
//		}
		

		// /////////////////////////////////////////////////////////////
		// Recupera la sesion abierta de facebook
		Session sesion = Session.getActiveSession();
		// Obtiene los datos del usuario de Facebook
		if (sesion != null) {
			Request.newMeRequest(sesion, new Request.GraphUserCallback() {

				// callback after Graph API response with user object
				@Override
				public void onCompleted(GraphUser user, Response response) {
					if (user != null) {
						textoPuntuacion = user.getFirstName()+" ";
						textoPuntuacion += getString(R.string.puntuacion_string)+" ";
						textoPuntuacion += GameApplication.Puntuacion+" ";
						puntuacion.setText(textoPuntuacion);
					}
				}
			}).executeAsync();
		}
		// /////////////////////////////////////////////////////////////

//		startActivityForResult(mGamesClient.getLeaderboardIntent(getString(R.string.leader_board_id_mejores_puntuaciones)), REQUEST_LEADERBOARD);
		
		
		buttonSi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onShowLeaderboardsRequested();
				seleccionarOpcion(true);

			}
		});

		buttonNo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onShowLeaderboardsRequested();
				seleccionarOpcion(false);

			}
		});
	}

	@Override
	public void onSignInFailed() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSignInSucceeded() {
		// TODO Auto-generated method stub
		getGamesClient().submitScore(getString(R.string.leader_board_id_mejores_puntuaciones), GameApplication.Puntuacion);
	}	
	
    public void onShowLeaderboardsRequested() {
        if (isSignedIn()) {
            startActivityForResult(getGamesClient().getLeaderboardIntent(getString(R.string.leader_board_id_mejores_puntuaciones)), REQUEST_LEADERBOARD);
        } else {
            showAlert("No leaderBoard disponible");
        }
    }
	
}
